﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum SpawnModes  //枚举类型，创造了两种生成模式
{
    Fixed,  //固定时间间隔
    Random   //随机时间间隔
}

public class Spawner : MonoBehaviour
{
    [Header("Settings")]
    [SerializeField] private SpawnModes spwanMode = SpawnModes.Fixed;
    [SerializeField] private int monsterCount = 10;
    [SerializeField] private float delayBtwWaves = 1f;
    [SerializeField] private bool ifSpawnAgain;
    //[SerializeField] private GameObject testGO;

    [Header("Fixed Delay")]
    [SerializeField] private float delayBtwSpawners;  //固定生成模式时间间隔

    [Header("Random Delay")]
    [SerializeField] private float minRondomDelay;    //随机生成模式最小生成时间与最大生成时间
    [SerializeField] private float maxRondomDelay;

    private float _spawnTimer;
    private float _monstersSpawned;
    private int _monstersRamaining;

    private ObjectPooler _pooler;
    private Waypoint _waypoint;


    private void Start()
    {
        _pooler = GetComponent<ObjectPooler>();
        _waypoint = GetComponent<Waypoint>();
        //GetComponent 方法是一种获取游戏对象上指定类型组件引用的常用手段，使得不同组件之间可以进行协同操作和数据交换。
        _monstersRamaining = monsterCount;
    }

    // Update is called once per frame
    void Update()  //在每一帧更新时，_spawnTimer 的值会减去 Time.deltaTime。Time.deltaTime 表示上一帧与当前帧之间的时间间隔，用于实现平滑的时间计算
    {
        _spawnTimer -= Time.deltaTime;
        if (_spawnTimer < 0)  //判断_spawnTimer小于0后，则开始执行后续操作
        {
            _spawnTimer = GetSpawnDelay();  //生成下一次出怪时间
            if (_monstersSpawned < monsterCount)  //如果已生成敌人数量_monstersSpawned小于给定生成的敌人数量monsterCount，则生成已敌人数量+1，并执行生成敌人的方法SpawnMonster()
            {
                _monstersSpawned++;
                SpawnMonster();
            }
        }
    }

    private void SpawnMonster()
    {
        GameObject newInstance = _pooler.GetInstanceFromPool();
        //Instantiate(newInstance, transform.position, Quaternion.identity); 
        //Instantiate() 函数来创建敌人对象的实例，三个参数：生成敌人的预制体，生成敌人的位置，生成敌人的旋转角度，Quaternion.identity表示没有旋转
        Monster monster = newInstance.GetComponent<Monster>();
        //如果 newInstance 游戏对象上存在 Monster 组件，那么 GetComponent<Monster>() 将返回该组件的引用，并将其赋值给 monster 变量
        monster.Waypoint = _waypoint;
        monster.ResetMonster();

        monster.transform.localPosition = transform.position;
        newInstance.SetActive(true);
    }

    private float GetSpawnDelay()  //返回生成敌人时间的函数
    {
        float delay = 0f;
        if (spwanMode == SpawnModes.Fixed)
        {
            delay = delayBtwSpawners;
        }
        else
        {
            delay = GetRondomDelay();
        }
        return delay;
    }

    private float GetRondomDelay()
    {
        float rondomTimer = Random.Range(minRondomDelay, maxRondomDelay);  
        return rondomTimer;
    }

    private IEnumerator NextWave()
    {
        yield return new WaitForSeconds(delayBtwWaves);
        _monstersRamaining = monsterCount;
        _spawnTimer = 0f;
        _monstersSpawned = 0;
    }

    private void RecordMonster()
    {
        _monstersRamaining--;
        if (_monstersRamaining <= 0 && ifSpawnAgain == true)
        {
            StartCoroutine(NextWave());
        }
    }

    private void OnEnable()
    {
        Monster.OnEndReached += RecordMonster;
        MonsterHealth.OnMonsterKilled += RecordMonster;
    }

    private void OnDisable()
    {
        Monster.OnEndReached -= RecordMonster;
        MonsterHealth.OnMonsterKilled -= RecordMonster;
    }
}